import {Client} from "@modelcontextprotocol/sdk/client/index.js";
import {SSEClientTransport} from "@modelcontextprotocol/sdk/client/sse.js";

// 创建客户端实例
const client = new Client({
    name: "example-client",
    version: "1.0.0"
}, {
    capabilities: {}
});
// 初始化 sse 传输实例
const transport = new SSEClientTransport(
    new URL("http://localhost:3000/sse")
);
// 连接到服务器
await client.connect(transport);

// 列出所有资源
const listResources = async () => {
    const resources = await client.listResources();
    console.log("列出所有资源：")
    console.log(resources)
}

// 读取指定资源
const readResource = async (uri: string) => {
    const resource = await client.readResource({uri: uri});
    console.log("读取指定资源：")
    console.log(resource)
}

// 订阅资源更新
client.fallbackNotificationHandler = async (notification) => {
    console.log("接收到通知：")
    console.log(notification)

    if (notification.method === "notifications/resources/list_changed") {
        console.log("收到资源列表更新通知，读取资源列表。")
        await listResources()
    }

    if (notification.method === "notifications/resources/updated") {
        console.log("收到资源内容更新通知，读取资源内容。")
        await readResource(notification.params!.uri as string)
    }
};